package com.sh.controller;


import com.sh.pojo.Result;
import com.sh.pojo.User;
import com.sh.pojo.UserPwd;
import com.sh.service.UserService;
import com.sh.utils.JwtUtil;
import com.sh.utils.ThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
@Slf4j
public class UseController {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserService userService;

    @PostMapping("register")
    public Result register(@RequestBody User user){
        //查询用户
        User u = userService.findByUserName(user.getUsername());
        if (u == null) {   //没有占用注册
            log.info("用户名："+user.getUsername()+"密码：{}"+user.getPassword());
            userService.register(user.getUsername(), user.getPassword());
            return Result.success();
        } else {
            //占用
            return Result.error("用户名已被占用");
        }
    }
    @PostMapping("login")
    public Result login(@RequestParam String username,@RequestParam String password){

        User u = userService.findByUserName(username);//查询用户
        if(u==null){//用户不存在
            return Result.error("用户不存在");

        }else{
            if(userService.login(username,password)){
                //生成token
                Map<String, Object> claims = new HashMap<>();
                claims.put("username",username);
                claims.put("id",u.getId());//id是通过用户名查询的
                String token=JwtUtil.genToken(claims);
                redisTemplate.opsForValue().set(token,token,1,TimeUnit.HOURS);
                //返回token
                return Result.success(token);
            }else{
                return Result.error("密码错误");
                //密码错误
            }
        }
    }
    @GetMapping("userinfo")
    public Result<User> info(){
        Map<String, Object> map = ThreadLocalUtil.get();//从线程中拿
        log.info("用户信息："+map);
        String username = (String) map.get("username");
        User user = userService.findByUserName(username);
        return Result.success(user);
    }
    @PutMapping("update")
    public Result update(@RequestBody User user){
        Map<String, Object> map = ThreadLocalUtil.get();//从线程中拿
        user.setId((Integer) map.get("id"));
        userService.update(user);
        return Result.success();
    }
    @PatchMapping("updateAvatar")
    public Result updateAvatar(@RequestParam String avatarUrl){
        Map<String, Object> map = ThreadLocalUtil.get();//从线程中拿
        Integer id = (Integer) map.get("id");
        User u= new User();
        u.setId(id);
        u.setUserPic(avatarUrl);
        userService.updateavtar(u);
        return Result.success();
    }
    @PatchMapping("updatePwd")
    public Result updatePassword(@RequestBody UserPwd userPwd){
        String old_pwd = userPwd.getOld_pwd();
        String newPwd = userPwd.getNew_pwd();
        String re_pwd = userPwd.getRe_pwd();
        if(!newPwd.equals(re_pwd)){
            return Result.error("两次密码不一致");
        }
        Map<String, Object> map = ThreadLocalUtil.get();//从线程中拿
        Integer id = (Integer) map.get("id");
        String username= (String) map.get("username");
        User u = userService.findByUserName(username);
        if(!u.getPassword().equals(old_pwd)){
            return Result.error("旧密码错误");
        }
        userService.updatePwd(id,newPwd);
        //生成token 废弃原来的
        Map<String, Object> claims = new HashMap<>();
        claims.put("username",username);
        claims.put("id",u.getId());//id是通过用户名查询的
        String token=JwtUtil.genToken(claims);
        redisTemplate.opsForValue().set("token:"+u.getId(),token,1,TimeUnit.HOURS);
        return Result.success();
    }
}
